/**
 * 
 */
package com.studio.app.dao.imp;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.type.StringType;
import org.hibernate.type.TimestampType;
import org.hibernate.type.Type;
import org.springframework.stereotype.Repository;

import com.studio.app.comm.dto.PageDto;
import com.studio.app.dao.LogDao;
import com.studio.app.model.LogInfo;


/**
 * @author EX-YUANQIANGHONG239
 *
 */
@Repository("logDao")
public class LogDaoImp extends BaseDaoImp implements LogDao{

	@Override
	public void insertLog(LogInfo log) {
	
		this.save(log);
	}

	@Override
	public List<LogInfo> queryLog(Map<String,Object> reqMap) {
		LogInfo log = (LogInfo)reqMap.get("log");
		StringBuilder hql1 = new StringBuilder("from LogInfo where 1=1");
		Object[] pObj = new Object[5];
		Type[] pType = new Type[5];
		if(StringUtils.isNotEmpty(log.getType())){
			hql1.append(" and type=?");
			pObj[0]=log.getType();
			pType[0] = new StringType();
		}
		if(StringUtils.isNotEmpty(log.getUserId())){
			hql1.append(" and userId=?");
			pObj[1]=log.getUserId();
			pType[1] = new StringType();
		}
		if(StringUtils.isNotEmpty(log.getContext())){
			hql1.append(" and context like '%"+log.getContext()+"%'");
		}
		
		PageDto pageDto = (PageDto)reqMap.get("pageDto");
		 

			
		if(log.getCreateDate() != null ){
			hql1.append(" and createDate>=? ");
			pObj[2] = log.getCreateDate();
			pType[2] = new TimestampType();
		}
		if(log.getEndDate() != null ){
			hql1.append(" and createDate<? ");
			pObj[3] = log.getEndDate();
			pType[3] = new TimestampType();
		}
		
		pageDto.setRecordCount(this.getTotalRecordNum("select count(logId) "+hql1.toString(),pObj,pType));
		hql1.append(" order by createDate desc");
		return this.getPageLists(pageDto, hql1.toString(), pObj,pType);
	}

	@Override
	public LogInfo queryLogById(int id) {
		
		return this.getHibernateTemplate().get(LogInfo.class, id);
	}

}
